<?php
namespace modules\potential;
use \modules\auth\api as auth;
use \phiction\array_utils as arr;
use \phiction\exceptions\bad_request;

class api
{
    static function add($q, &$args)
    {
        $name     = arr::get_or_bad_request($args, 'name');
        $priority = (int)arr::get_or_bad_request($args, 'priority');
        $created  = new \DateTime();
        $comment  = arr::get_or($args, 'comment', '');
        $contact  = arr::get_or_bad_request($args, 'contact-id');

        $user = auth::validate_with_role($q, $args, "salesman");
        $responsible = $user['id'];
        $org  = $user['org'];

        if(storage::add($q, $name, $priority, $created, $comment,
                $org, $responsible, $contact)->run() !== 1)
            throw new bad_request("添加商机失败");
    }

    static function edit($q, &$args)
    {
        $id       = (int)arr::get_or_bad_request($args, 'id');
        $priority = (int)arr::get_or_bad_request($args, 'priority');
        $name     = arr::get_or_bad_request($args, 'name');
        $comment  = arr::get_or($args, 'comment', '');

        $user = auth::validate_with_role($q, $args, "salesman");
        $responsible = $user['id'];
        $org  = $user['org'];

        if(storage::edit($q, $id, $name, $priority, $comment,
                $responsible, $org)->run() !== 1)
            throw new bad_request("编辑商机失败");
    }
    static function list_active($q, &$args)
    {
        $page = (int)arr::get_or_bad_request($args, 'page');

        $user = auth::validate_with_role($q, $args, "salesman");
        $org  = $user['org'];

        $list = storage::list_by_active($q, $org, $page, true)->run();
        return $list;
    }

    static function list_archived($q, &$args)
    {
        $page = (int)arr::get_or_bad_request($args, 'page');

        $user = auth::validate_with_role($q, $args, "salesman");
        $org  = $user['org'];

        $list = storage::list_by_active($q, $org, $page, false)->run();
        return $list;
    }

    static function archive($q, &$args)
    {
        $id = (int)arr::get_or_bad_request($args, 'id');

        $user = auth::validate_with_role($q, $args, 'salesman');
        $org  = $user['org'];
        $uid  = $user['id'];

        if (storage::active($q, $org, $id, false, $uid)->run() !== 1)
            throw new bad_request("关闭商机失败");
    }

    static function active($q, &$args)
    {
        $id = (int)arr::get_or_bad_request($args, 'id');

        $user = auth::validate_with_role($q, $args, 'salesman');
        $org  = $user['org'];
        $uid  = $user['id'];

        if (storage::active($q, $org, $id, true, $uid)->run() !== 1)
            throw new bad_request("恢复商机失败");
    }

    static function potential_profile($q, &$args)
    {
        $id = (int)arr::get_or_bad_request($args, 'id');

        $user = auth::validate_with_role($q, $args, "salesman");
        $org  = $user['org'];

        $profile = storage::profile($q, $org, $id)->run();
        if (is_null($profile))
            throw new bad_request("无此商机");

        return $profile;
    }

    static function list_by_contact($q, &$args)
    {
        $contact = (int)arr::get_or_bad_request($args, 'id');

        $user = auth::validate_with_role($q, $args, "salesman");
        $org  = $user['org'];

        $list = storage::list_by_contact($q, $org, $contact)->run();
        return $list;
    }

    static function list_my_potential($q, &$args)
    {
        $user = auth::validate_with_role($q, $args, "salesman");
        $responsible = $user["id"];
        $org  = $user['org'];

        $list = storage::list_my_potential($q, $org, $responsible)->run();
        return $list;
    }
}

